home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / ASN.java < prev    next >
Text File  |  1997-06-09  |  4KB  |  118 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. /**
  18.  * Class encapsulating ASN constants and miscellanous ASN routines.
  19.  * @version     $Id: ASN.java,v 1.3 1997/05/08 13:04:46 alex Exp $
  20.  * @author      Alex Kowalenko
  21.  */
  22.  
  23. public class ASN {
  24.     
  25.   public static final byte BOOLEAN =      (byte) 0x01;
  26.   public static final byte INTEGER =      (byte) 0x02;
  27.   public static final byte BIT_STR =      (byte) 0x03;
  28.   public static final byte OCTET_STR =    (byte) 0x04;
  29.   public static final byte NULL =         (byte) 0x05;
  30.   public static final byte OBJECT_ID =    (byte) 0x06;
  31.   public static final byte SEQUENCE =     (byte) 0x10;
  32.   public static final byte SET =          (byte) 0x11;
  33.     
  34.   public static final byte UNIVERSAL =     (byte) 0x00;
  35.   public static final byte APPLICATION =   (byte) 0x40;
  36.   public static final byte CONTEXT =       (byte) 0x80;
  37.   public static final byte PRIVATE =       (byte) 0xC0;
  38.     
  39.   public static final byte PRIMITIVE =     (byte) 0x00;
  40.   public static final byte CONSTRUCTOR =   (byte) 0x20;
  41.     
  42.   public static final byte LONG_LEN =      (byte) 0x80;
  43.   public static final byte EXTENSION_ID =  (byte) 0x1F;
  44.   public static final byte BIT8 =          (byte) 0x80;
  45.     
  46.   public static final byte IPADDRESS =    (byte) 0x40;
  47.   public static final byte COUNTER =      (byte) 0x41;
  48.   public static final byte TIMETICKS =    (byte) 0x43;
  49.   public static final byte GAUGE =        (byte) 0x42;
  50.   
  51. /**
  52.  * Returns the ASN encoding of length 
  53.  * @see aka.snmp.ASN#getLength
  54.  */
  55.  
  56.   public static ByteBuffer buildLength(int length) {
  57.       ByteBuffer result = new ByteBuffer();
  58.       if(length < 0x80) {
  59.       result.append( (byte) length);
  60.       }
  61.       else if(length <= 0xFF) {
  62.       result.append( (byte) (0x01 | LONG_LEN));
  63.       result.append( (byte) length);
  64.       }
  65.       else { // 0xFF < length <= 0xFFFF 
  66.       result.append( (byte) (0x02 | LONG_LEN));
  67.       result.append( (byte) ((length >> 8) & 0xFF));
  68.       result.append( (byte) (length & 0xFF));
  69.       }
  70.       return result;
  71.   };
  72.  
  73. /**
  74.  * Returns the length from the ASN encoding in buffer
  75.  * @see aka.snmp.ASN#buildLength
  76.  */
  77.   
  78.   public static int getLength(ByteBuffer buffer) {
  79.       byte var = buffer.byteAt(0);
  80.       int result;
  81.       switch(var) {
  82.     case (byte) 0x01 | LONG_LEN:
  83.       result = buffer.byteAt(1);
  84.       result = (result < 0) ? result + 256 : result;
  85.       buffer.removeBeginning(2);
  86.       return result;
  87.     case (byte) 0x02 | LONG_LEN:
  88.       result = buffer.byteAt(1);
  89.       result = (result < 0) ? result + 256 : result;
  90.       result <<= 8;
  91.       result = result | ( buffer.byteAt(2) > 0 ? buffer.byteAt(2) :  buffer.byteAt(2) +256);
  92.       buffer.removeBeginning(3);
  93.       return result;
  94.     default:
  95.       result = var;
  96.       buffer.removeBeginning(1);
  97.       }
  98.       return result;
  99.   };
  100.  
  101.   private ASN() {};
  102.  
  103. /**
  104.  * Test Harness
  105.  */
  106.  
  107.   public static void main(String args[]) {
  108.       StringUtil.Hexdump(buildLength(16));
  109.       StringUtil.Hexdump(buildLength(250));
  110.       StringUtil.Hexdump(buildLength(1000));
  111.       
  112.       System.out.println(getLength(buildLength(16)));
  113.       System.out.println(getLength(buildLength(250)));
  114.       System.out.println(getLength(buildLength(1000)));
  115.   };
  116.  
  117. };
  118.